package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.mapper.RoleMapper;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class RoleServiceimpl implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void deleteByPrimaryKey(Long id) {
        //删除中间表
        roleMapper.delectRelation(id);
        roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insert(Role role,Long[] ids) {

        roleMapper.insert(role);
        if (ids != null && ids.length >0) {
            for (Long Permissionid : ids) {
                roleMapper.insertRelation(role.getId(),Permissionid);
            }
        }
    }

    @Override
    public Role selectByPrimaryKey(Long id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Role> selectAll() {
        return roleMapper.selectAll();
    }

    @Override
    public void updateByPrimaryKey(Role role, Long[] ids) {

        roleMapper.updateByPrimaryKey(role);
        //删除中间表
        roleMapper.delectRelation(role.getId());
        if (ids != null && ids.length >0) {
            for (Long Permissionid : ids) {
                roleMapper.insertRelation(role.getId(),Permissionid);
            }
        }
    }

    @Override
    public PageInfo<Role> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Role> list = roleMapper.queryForList(qo);
        return new PageInfo<>(list);

    }

}
